/**
 * 
 */
package com.douyu.ocean.recom.platform.api.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;

import com.douyu.ocean.recom.platform.api.model.OceanResultSource;

/**
 * @author macbook
 *
 */
public class RedisBizSourceUtil {

	private static Map<String,OceanResultSource> map = new HashMap<String,OceanResultSource>();
	
	public static void setSource(String bizKey,OceanResultSource source){
		map.put(bizKey, source);
	}
	
	public static OceanResultSource getSource(String bizKey){
		return map.get(bizKey);
	}
	
	
	public static RedisTemplate<?,?> getRedisTemplateBySourceBizKey(String bizKey){
		OceanResultSource source = getSource(bizKey);
		if(source !=null){
			return RedisClientUtil.getRedisTemplateByKey(source.getSourceKey());
		}
		return null;
	}
	
	public static RedisTemplate<?,?> getRedisTemplateBySource(OceanResultSource source){
		RedisTemplate<?,?> redisTemplate = getRedisTemplateBySourceBizKey(source.getBizKey());
		if(redisTemplate == null){
			redisTemplate =  RedisClientUtil.getRedisTemplateByKey(source.getSourceKey());
		}
		return redisTemplate;
	}
	
	public static List<OceanResultSource> getBizSourceListBySourceStr(String sourceStr){
		String[] bizSourceArrayStr = StringUtils.split(sourceStr,",");
		List<OceanResultSource> bizSourceList = new ArrayList<OceanResultSource>();
		for(String name:bizSourceArrayStr){
			String[] source = StringUtils.split(name, "|");
			if(source.length==1){
				bizSourceList.add(new OceanResultSource(source[0]));
			}
			else if(source.length==2){
				bizSourceList.add(new OceanResultSource(source[0],source[1]));
			}
		}
		return bizSourceList;
	}
	
	
}
